home *** CD-ROM | disk | FTP | other *** search
/ Ham Radio 2000 / Ham Radio 2000.iso / ham2000 / tcp_ip / gp / extract.c < prev    next >
C/C++ Source or Header  |  1991-12-16  |  3KB  |  123 lines

  1. #include "7plus.h"
  2. #include "globals.h"
  3.  
  4. /*
  5. *** extract 7plus-files from log-file.
  6. ***
  7. ***
  8. ***
  9.  */
  10.  
  11. int extract_files (char *name, char *search)
  12. {
  13.   FILE *rein, *raus;
  14.   char string[81], destnam[13], dummi[20], *p, *q;
  15.   int  part, file, err, errn;
  16.  
  17.   raus = NULL;
  18.   file = err = errn = 0;
  19.  
  20.   if (search)
  21.     strlwr (search);
  22.  
  23.   printf ("*** 7PLUS file extractor ***\n");
  24.  
  25.   if ((rein = fopen (name, OPEN_READ_BINARY)) == NULL)
  26.   {
  27.     printf (cant, name);
  28.     return (2);
  29.   }
  30.   setvbuf (rein, NULL, _IOFBF, buflen);
  31.  
  32.   printf ("Scanning %s for 7PLUS-files.\n\n", name);
  33.  
  34.   while ((p = my_fgets (string, 80, rein)) != NULL)
  35.   {
  36.     if (!strncmp (p, " go_", 4)) /* Beginning of a 7PLUS file. */
  37.     {
  38.       if (raus)
  39.       {
  40.         fclose (raus);
  41.         raus = NULL;
  42.       }
  43.       *destnam = EOS;
  44.       if (!strncmp (p+4, "7+.", 3)) /* It's a code file */
  45.       {
  46.         /* Get filename from header. Create output filename. */
  47.         sscanf (p+7, "%d%s%s%s", &part, dummi, dummi, destnam);
  48.         if ((q = strrchr (destnam, '.')) != NULL)
  49.           *q = EOS;
  50.         destnam[8] = EOS;
  51.         if (strstr (p, "of 001"))
  52.           sprintf (dummi, ".7pl");
  53.         else
  54.           sprintf (dummi, ".p%02x", part);
  55.         strcat (destnam, dummi);
  56.       }
  57.       /* OK, it's an ERR or COR file. */
  58.       if (!strncmp (p+4, "text.", 5) &&
  59.           (strstr (p, ".ERR") || strstr (p, ".COR")))
  60.         sscanf (p+10, "%12s", destnam);
  61.       strlwr (destnam);
  62.  
  63.       err = 0;
  64.       if (strstr (p, ".ERR"))
  65.         err = 1;
  66.  
  67.       if (search && *destnam && !strstr (destnam, search))
  68.       {
  69.         printf ("Not extracted: %s\n", destnam);
  70.         *destnam = EOS;
  71.       }
  72.       if (*destnam) /* Open output file if 7PLUS file found. */
  73.       {
  74.         if (err && !test_exist (destnam))
  75.         {
  76.           errn = 1;
  77.           do
  78.           {
  79.             if ((q = strrchr (destnam, '.')) != NULL)
  80.               *q = EOS;
  81.             sprintf (dummi, ".e%02x", errn++);
  82.             strcat (destnam, dummi);
  83.           }
  84.           while (!test_exist (destnam));
  85.         }
  86.  
  87.         file++;
  88.         printf ("Extracting %s\n", destnam);
  89.         test_file (rein, destnam, 1, 12);
  90.         raus = fopen (destnam, OPEN_WRITE_TEXT);
  91.         setvbuf (raus, NULL, _IOFBF, buflen);
  92.       }
  93.     }
  94.     if (raus)
  95.       if (fprintf (raus, "%s", p) == EOF)
  96.       {
  97.         printf ("\007\nWrite error. Can't continue. Break.\n");
  98.         exit (1);
  99.       }
  100.  
  101.     if (!strncmp (p, " stop_", 6) && raus) /* End of file reached? */
  102.     {
  103.       fclose (raus);
  104.       raus = NULL;
  105.     }
  106.   }
  107.  
  108.   printf ("\n");
  109.  
  110.   if (file)
  111.     printf ("All done!\n");
  112.   else
  113.     if (search)
  114.       printf ("No matching 7PLUS files found in %s.\n", name);
  115.     else
  116.       printf ("No 7PLUS files found in %s.\n", name);
  117.  
  118.   fclose (rein);
  119.   if (raus)
  120.     fclose (raus);
  121.   return (0);
  122. }
  123.